package com.dong.drool.autoconfigure;

import com.dong.drool.autoconfigure.core.DefaultKieDroolsExecutor;
import com.dong.drool.autoconfigure.core.DroolExecuteService;
import com.dong.drool.autoconfigure.core.KieDroolExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * @author zhaodongchao
 * @date 2019/9/18 11:06
 */
@Configuration
@EnableConfigurationProperties(value = {DroolProperties.class})
@ConditionalOnProperty(prefix = DroolProperties.DROOL_PREFIX, name = "enabled", havingValue = "true")
public class DroolAutoConfiguration {
    private static final Logger logger = LoggerFactory.getLogger(DroolAutoConfiguration.class);

    private final DroolProperties properties;

    private final List<ConfigurationCustomizer> configurationCustomizers;

    public DroolAutoConfiguration(DroolProperties properties, ObjectProvider<List<ConfigurationCustomizer>> configurationCustomizers) {
        this.properties = properties;
        this.configurationCustomizers = configurationCustomizers.getIfAvailable();
    }

    @Bean
    @ConditionalOnMissingBean(value = {DroolExecuteService.class})
    public KieDroolExecutor kieDroolExecutor() {
        KieDroolExecutor kieDroolExecutor = new DefaultKieDroolsExecutor(properties.debugFlag);
        logger.info("drool初始化成功！");
        return kieDroolExecutor;
    }
}
